package com.jcrj.service;

import com.jcrj.dao.MenuDao;
import com.jcrj.entity.OMenu;
import com.jcrj.entity.ORoleMenu;
import java.util.List;

public class MenuService
{
  private StringBuffer buff;
  private MenuDao menuDao;

  public MenuDao getMenuDao()
  {
    return this.menuDao;
  }

  public void setMenuDao(MenuDao menuDao) {
    this.menuDao = menuDao;
  }

  public List<OMenu> getUpMenu(String userId) {
    String hql = "from OMenu m where m.id in (select distinct(r.menuId)  from ORoleUser u,ORoleMenu r where u.roleId=r.roleId and u.userId in(" + userId + ")) and m.pid=0 order by px asc";
    return this.menuDao.findByHql(hql);
  }

  public String getMenuTree(int id, int userId) {
    this.buff = new StringBuffer();
    String hql = "from OMenu t where t.id in (select distinct(r.menuId)  from ORoleUser u,ORoleMenu r where u.roleId=r.roleId and u.userId=" + userId + ") and t.pid=" + id + " order by px asc";
    List<OMenu> list = this.menuDao.findByHql(hql);
    for (OMenu menu : list) {
      this.buff.append("<li class=\"L1\"><a href=\"javascript:c(" + menu.getId() + ");\" id=\"" + menu.getId() + "\"><span><img src=\"image/2.gif\" align=\"absMiddle\"/>").append(menu.getName()).append("</span></a></li>");
      this.buff.append("<ul id=\"" + menu.getId() + "d\" >");
      getChildMenu(menu.getId().intValue(), userId);
      this.buff.append("</ul>");
      this.buff.append("</li>");
    }
    return this.buff.toString();
  }

  private String getChildMenu(int id, int userId) {
    String hql = "from OMenu t where t.id in (select distinct(r.menuId)  from ORoleUser u,ORoleMenu r where u.roleId=r.roleId and u.userId=" + userId + ") and t.pid=" + id + " order by px asc";
    List list = this.menuDao.findByHql(hql);
    if ((list != null) && (!list.isEmpty())) {
      for (int i = 0; i < list.size(); i++) {
        OMenu menu = (OMenu)list.get(i);
        if ((menu.getUrl() != null) && (!"".equals(menu.getUrl()))) {
          this.buff.append("<li class=\"L22\"><a href=" + menu.getUrl() + " target=\"rightFrame\" ><span><img src=\"image/2.gif\" align=\"absMiddle\"/>").append(menu.getName()).append("</span></a>");
        } else {
          this.buff.append("<li class=\"L21\"><a href=\"javascript:c(" + menu.getId() + ");\" id=" + menu.getId() + "><span><img src=\"image/2.gif\" align=\"absMiddle\"/>").append(menu.getName()).append("</span></a>");
          this.buff.append("<ul id=\"" + menu.getId() + "d\" style=\"display:none;\">");
          getChildMenu(menu.getId().intValue(), userId);
          this.buff.append("</ul>");
        }
        this.buff.append("</li>");
      }
    }

    return this.buff.toString();
  }
  public String getAllMenuTree() {
    this.buff = new StringBuffer();
    String hql = "from OMenu t where t.pid=0 order by t.px asc";
    List<OMenu> list = this.menuDao.findByHql(hql);
    for (OMenu menu : list) {
      getString(menu);
      getchild(menu.getId().intValue());
      this.buff.append("</li>");
    }
    return this.buff.toString();
  }
  private String getString(OMenu m) {
    if (m != null) {
      this.buff.append("<li>").append("<input type='checkbox' name='menus' id='geo' value='" + m.getId() + "'><label>").append(m.getName()).append("</label>");
    }
    return this.buff.toString();
  }

  public String getchild(int id) {
    String hql = "from OMenu t where t.pid=" + id + "  order by t.px asc";
    List list = this.menuDao.findByHql(hql);
    if ((list != null) && (!list.isEmpty())) {
      this.buff.append("<ul>");
    }
    for (int i = 0; i < list.size(); i++) {
      OMenu m = (OMenu)list.get(i);
      getString(m);
      getchild(m.getId().intValue());
      this.buff.append("</li>");
    }
    if ((list != null) && (!list.isEmpty())) {
      this.buff.append("</ul>");
    }
    return this.buff.toString();
  }

  public List<ORoleMenu> getRoleMenu(int roleId) {
    String hql = "from ORoleMenu o where o.roleId=" + roleId;
    return this.menuDao.findByHql(hql);
  }

  public void saveRoleMenu(int roleId, String[] menus) {
    String hql = "delete from ORoleMenu where roleId=" + roleId;
    this.menuDao.executeHql(hql);
    if ((menus != null) && (menus.length > 0))
      for (String menuId : menus) {
        ORoleMenu om = new ORoleMenu();
        om.setMenuId(Integer.valueOf(Integer.parseInt(menuId)));
        om.setRoleId(Integer.valueOf(roleId));
        this.menuDao.saveRoleMenu(om);
      }
  }
}

/* Location:           /Volumes/work/oa/WEB-INF/classes/
 * Qualified Name:     com.jcrj.service.MenuService
 * JD-Core Version:    0.6.0
 */